package sms

import (
	"fmt"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	dysmsapi "github.com/alibabacloud-go/dysmsapi-20180501/v2/client"
	"github.com/alibabacloud-go/tea/tea"
	"private-domain-overseas-service/pkg/xerr"
)

type GlobeSmsService struct {
	AccessKeyId     string //凭证id
	AccessKeySecret string //密钥
	Endpoint        string //服务器地址
}

func NewGlobeSmsService() (*GlobeSmsService, error) {
	return &GlobeSmsService{
		AccessKeyId:     "LTAI5tLZu4eLEVp9Z1BYGGmi",
		AccessKeySecret: "Y5616mvPbnRFwAngGVNSPHIrAXscwZ",
		Endpoint:        "dysmsapi.ap-southeast-1.aliyuncs.com",
	}, nil
}

// GlobeSendMessage - 发送国际/港澳台短信接口
func (l *GlobeSmsService) GlobeSendMessage(phone, msg string, env string) error {
	//正式服才发送短信
	//if env != "pro" {
	//	return nil
	//}

	config := &openapi.Config{
		// 您的AccessKey ID
		AccessKeyId: tea.String(l.AccessKeyId),
		// 您的AccessKey Secret
		AccessKeySecret: tea.String(l.AccessKeySecret),
	}
	config.Endpoint = tea.String(l.Endpoint)
	client, _ := dysmsapi.NewClient(config)
	request := &dysmsapi.SendMessageToGlobeRequest{}

	request.SetTo(phone)
	request.SetMessage(msg)
	_, err := client.SendMessageToGlobe(request)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("发送验证码失败：%v", err))
	}
	return nil
}
